package com.co.user.service.impl;

import com.co.common.util.IdWorker;
import com.co.user.mapper.CommentMapper;
import com.co.comment.pojo.Comment;
import com.co.user.service.CommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * @author ldl.plus
 * @date 2020年06月22日  0:41
 * ZhComment业务层接口实现类
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private IdWorker idWorker;

    /**
     * ZhComment条件+分页查询
     *
     * @param comment 查询条件
     * @param page    页码
     * @param size    页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Comment> findPage(Comment comment, int page, int size) {
        //分页
        PageHelper.startPage(page, size);
        //搜索条件构建
        Example example = createExample(comment);
        //执行搜索
        return new PageInfo<Comment>(commentMapper.selectByExample(example));
    }

    /**
     * ZhComment分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Comment> findPage(int page, int size) {
        //静态分页
        PageHelper.startPage(page, size);
        //分页查询
        return new PageInfo<Comment>(commentMapper.selectAll());
    }

    /**
     * ZhComment条件查询
     *
     * @param comment
     * @return
     */
    @Override
    public List<Comment> findList(Comment comment) {
        //构建查询条件
        Example example = createExample(comment);
        //根据构建的条件查询数据
        return commentMapper.selectByExample(example);
    }


    /**
     * ZhComment构建查询对象
     *
     * @param comment
     * @return
     */
    public Example createExample(Comment comment) {
        Example example = new Example(Comment.class);
        Example.Criteria criteria = example.createCriteria();
        if (comment != null) {
            // 评论主键
            if (!StringUtils.isEmpty(comment.getId())) {
                criteria.andEqualTo("id", comment.getId());
            }
            // 用户主键id
            if (!StringUtils.isEmpty(comment.getUsername())) {
                criteria.andLike("username", "%" + comment.getUsername() + "%");
            }
            // vdetail的主键
            if (!StringUtils.isEmpty(comment.getAid())) {
                criteria.andEqualTo("aid", comment.getAid());
            }
            // 评论点赞数
            if (!StringUtils.isEmpty(comment.getLike())) {
                criteria.andEqualTo("like", comment.getLike());
            }
            // 评论点踩数
            if (!StringUtils.isEmpty(comment.getDislike())) {
                criteria.andEqualTo("dislike", comment.getDislike());
            }
            // 父id，回复的评论
            if (!StringUtils.isEmpty(comment.getParentId())) {
                criteria.andEqualTo("parentId", comment.getParentId());
            }
            // 评论内容
            if (!StringUtils.isEmpty(comment.getText())) {
                criteria.andEqualTo("text", comment.getText());
            }
            // 创建时间
            if (!StringUtils.isEmpty(comment.getCreateTime())) {
                criteria.andEqualTo("createTime", comment.getCreateTime());
            }
            // 修改时间
            if (!StringUtils.isEmpty(comment.getUpdateTime())) {
                criteria.andEqualTo("updateTime", comment.getUpdateTime());
            }
        }
        return example;
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(String id) {
        commentMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改ZhComment
     *
     * @param comment
     */
    @Override
    public void update(Comment comment) {
        commentMapper.updateByPrimaryKey(comment);
    }

    /**
     * 增加ZhComment
     *
     * @param comment
     */
    @Override
    public void add(Comment comment) {
        commentMapper.insertSelective(comment);
    }

    /**
     * 根据ID查询ZhComment
     *
     * @param id
     * @return
     */
    @Override
    public Comment findById(String id) {
        return commentMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询ZhComment全部数据
     *
     * @return
     */
    @Override
    public List<Comment> findAll() {
        return commentMapper.selectAll();
    }
}
